using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using SqlSugar;
using SqlSugarEx;
using SqlSugarEx.Utilities;

namespace Catastrophe.Log
{
	public class SysOprRecordsDal
	{
		private static SqlSugarClient _sugarEx;

		private static object LOCK = new object();

		private static SqlSugarClient SqlSugarEx => SysOprRecordsDal._sugarEx ?? (SysOprRecordsDal._sugarEx = DbUtility.GetSugar(Catastrophe.Initialize.SystemIni.FileConfigIni.GetDbAppConfig));

		public static List<SysOprRecordsEntity> QueryRecords(string operatorStr, string module, string detail, string startTime, string endTime)
		{
			if (!string.IsNullOrWhiteSpace(startTime))
			{
				startTime = DateTime.Parse(startTime).ToString("yyyy/MM/dd");
			}
			if (!string.IsNullOrWhiteSpace(endTime))
			{
				endTime = DateTime.Parse(endTime).ToString("yyyy/MM/dd");
			}
			return (from x in SysOprRecordsDal.SqlSugarEx.Queryable<SysOprRecordsEntity>().WhereIF(!string.IsNullOrWhiteSpace(operatorStr), (SysOprRecordsEntity x) => x.Operator.Contains(operatorStr))
				where x.SoftId == LogSession.SoftId
				select x).WhereIF(!string.IsNullOrWhiteSpace(module), (SysOprRecordsEntity x) => x.Module.Contains(module)).WhereIF(!string.IsNullOrWhiteSpace(detail), (SysOprRecordsEntity x) => x.Details.Contains(detail)).WhereIF(!string.IsNullOrEmpty(startTime) && !string.IsNullOrEmpty(endTime), (SysOprRecordsEntity x) => SqlFunc.Between(x.TimeStamp, startTime, endTime))
				.ToList();
		}

		public static void AddRecords(SysOprRecordsEntity entity)
		{
			lock (SysOprRecordsDal.LOCK)
			{
				entity.SoftId = LogSession.SoftId;
				entity.TimeStamp = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
				Task<int> task = SysOprRecordsDal.SqlSugarEx.Insertable(entity).ExecuteCommandAsync();
			}
		}

		public static void AddRecords(string module, string details, string result)
		{
			SysOprRecordsDal.AddRecords(new SysOprRecordsEntity
			{
				Operator = "",
				Module = module,
				Details = details,
				Result = result,
				SoftId = LogSession.SoftId
			});
		}

		public static void AddRecords(string user, string module, string details, string result)
		{
			SysOprRecordsDal.AddRecords(new SysOprRecordsEntity
			{
				Operator = user,
				Module = module,
				Details = details,
				Result = result,
				SoftId = LogSession.SoftId
			});
		}

		public static void ClearOprRecords(DateTime? startTime, DateTime? endTime)
		{
			if (!startTime.HasValue || !endTime.HasValue)
			{
				SysOprRecordsDal.SqlSugarEx.Deleteable<SysOprRecordsEntity>().ExecuteCommand();
				return;
			}
			IDeleteable<SysOprRecordsEntity> deleteable = SysOprRecordsDal.SqlSugarEx.Deleteable<SysOprRecordsEntity>();
			DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(31, 2);
			defaultInterpolatedStringHandler.AppendLiteral("TimeStamp>='");
			defaultInterpolatedStringHandler.AppendFormatted(startTime);
			defaultInterpolatedStringHandler.AppendLiteral("' and TimeStamp<='");
			defaultInterpolatedStringHandler.AppendFormatted(endTime);
			defaultInterpolatedStringHandler.AppendLiteral("'");
			deleteable.Where(defaultInterpolatedStringHandler.ToStringAndClear()).ExecuteCommand();
		}

		public static void ClearOprRecords(SqlSugarClientEx sugarEx)
		{
			sugarEx.Deleteable<SysOprRecordsEntity>().ExecuteCommand();
		}
	}
}
